package com.stockz.watchlist.model;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.PrePersist;
import javax.persistence.SequenceGenerator;

import com.stockz.core.model.Scrip;

@Entity
@NamedQueries(value={
		@NamedQuery(name=WatchListEntry.getEntriesByWatchListIdQuery, query="from WatchListEntry where watchList.id = :id")
})
public class WatchListEntry implements Serializable,Comparable<WatchListEntry> {
	private static final long serialVersionUID = 2348652944830868632L;

	public static final String getEntriesByWatchListIdQuery = "getEntriesByWatchListIdQuery";
	
	private Long id;
	private WatchList watchList;
	private Scrip scrip;
	private String description;
	private Date addedDate;
	private Date inspectionDate;
	
	@Override
	public int compareTo(WatchListEntry o) {
		if(o != null && o.getScrip() != null && o.getScrip().getCode() != null){
			return scrip.getCode().compareTo(o.getScrip().getCode());
		}
		return 0;
	}
	
	
	@Id
	@Column
	@GeneratedValue(strategy = GenerationType.SEQUENCE)
	@SequenceGenerator(name = "WatchListEntryIdSequence", sequenceName = "WatchListEntryIdSequence")
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	
	@ManyToOne(optional=false,targetEntity=Scrip.class)
	//@JoinColumn(name="code",table="WatchListEntry")
	public Scrip getScrip() {
		return scrip;
	}
	public void setScrip(Scrip scrip) {
		this.scrip = scrip;
	}
	
	@Column
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	
	@Column(nullable=false)
	public Date getAddedDate() {
		return addedDate;
	}

	@PrePersist
	public void prePersist(){
		if(addedDate == null){
			addedDate = new Date();
		}
	}
	
	@Column(nullable=false)
	public Date getInspectionDate() {
		return inspectionDate;
	}
	public void setInspectionDate(Date inspectionDate) {
		this.inspectionDate = inspectionDate;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((addedDate == null) ? 0 : addedDate.hashCode());
		result = prime * result
				+ ((description == null) ? 0 : description.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result
				+ ((inspectionDate == null) ? 0 : inspectionDate.hashCode());
		result = prime * result + ((scrip == null) ? 0 : scrip.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		WatchListEntry other = (WatchListEntry) obj;
		if (addedDate == null) {
			if (other.addedDate != null)
				return false;
		} else if (!addedDate.equals(other.addedDate))
			return false;
		if (description == null) {
			if (other.description != null)
				return false;
		} else if (!description.equals(other.description))
			return false;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (inspectionDate == null) {
			if (other.inspectionDate != null)
				return false;
		} else if (!inspectionDate.equals(other.inspectionDate))
			return false;
		if (scrip == null) {
			if (other.scrip != null)
				return false;
		} else if (!scrip.equals(other.scrip))
			return false;
		return true;
	}

	@SuppressWarnings("unused")
	private void setAddedDate(Date addedDate) {
		this.addedDate = addedDate;
	}

	@ManyToOne
	@JoinColumn(name="watchListId")
	public WatchList getWatchList() {
		return watchList;
	}

	public void setWatchList(WatchList watchList) {
		this.watchList = watchList;
	}
}
